<template>
  <view class="container">
    <view class="content">
      <!-- 睡眠记录统计 -->
      <view class="stats-card">
        <view class="stats-header">
          <text class="stats-title">睡眠概览</text>
          <text class="stats-date">{{ currentMonth }}</text>
        </view>
        <view class="stats-body">
          <view class="stats-item">
            <text class="stats-value">{{ averageDuration }}</text>
            <text class="stats-label">平均睡眠时长</text>
          </view>
          <view class="stats-item">
            <text class="stats-value">{{ averageQuality }}</text>
            <text class="stats-label">平均睡眠质量</text>
          </view>
          <view class="stats-item">
            <text class="stats-value">{{ recordCount }}</text>
            <text class="stats-label">记录天数</text>
          </view>
        </view>
      </view>

      <!-- 睡眠记录列表 -->
      <view class="record-list">
        <view class="record-header">
          <text class="section-title">睡眠记录</text>
          <view class="add-record" @click="addRecord">
            <text class="add-icon">➕</text>
            <text>新增记录</text>
          </view>
        </view>
        
        <view class="empty-tip" v-if="sleepRecords.length === 0 && !loading">
          <text>暂无睡眠记录</text>
          <button class="btn-primary" @click="addRecord">开始记录</button>
        </view>
        
        <view v-else v-for="(item, index) in sleepRecords" :key="index" class="record-item">
          <view class="record-card">
            <view class="record-date">
              <text class="date-day">{{ formatDay(item.date) }}</text>
              <text class="date-month">{{ formatMonth(item.date) }}</text>
            </view>
            <view class="record-info">
              <view class="sleep-time">
                <text class="time-label">入睡时间</text>
                <text class="time-value">{{ item.bedTime }}</text>
              </view>
              <view class="sleep-time">
                <text class="time-label">起床时间</text>
                <text class="time-value">{{ item.wakeTime }}</text>
              </view>
              <view class="sleep-duration">
                <text class="duration-label">睡眠时长</text>
                <text class="duration-value">{{ item.duration }}</text>
              </view>
            </view>
            <view class="record-quality">
              <text class="quality-label">质量</text>
              <view class="quality-stars">
                <text v-for="i in 5" :key="i" class="star-icon" :style="{ color: i <= item.quality ? '#FFB800' : '#ccc' }">
                  {{ i <= item.quality ? '⭐' : '☆' }}
                </text>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 加载更多 -->
        <view class="loading-more" v-if="loading">
          <view class="loading-content">
            <text class="loading-text">加载中...</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="tab-bar">
      <view class="tab-item" @click="handleTabClick('home')">
        <text class="tab-icon">🏠</text>
        <text class="tab-text">首页</text>
      </view>
      <view class="tab-item" @click="handleTabClick('survey')">
        <text class="tab-icon">📋</text>
        <text class="tab-text">问卷</text>
      </view>
      <view class="tab-item active" @click="handleTabClick('record')">
        <text class="tab-icon" style="color: #2979ff;">✅</text>
        <text class="tab-text" style="color: #2979ff;">记录</text>
      </view>
      <view class="tab-item" @click="handleTabClick('knowledge')">
        <text class="tab-icon">📚</text>
        <text class="tab-text">知识</text>
      </view>
      <view class="tab-item" @click="handleTabClick('my')">
        <text class="tab-icon">👤</text>
        <text class="tab-text">我的</text>
      </view>
    </view>
  </view>
</template>

<script>
import { ref, computed, onMounted } from 'vue';

export default {
  setup() {
    // 示例数据
    const sleepRecords = ref([
      {
        id: 1,
        date: '2023-06-01',
        bedTime: '23:30',
        wakeTime: '07:30',
        duration: '8小时',
        quality: 4
      },
      {
        id: 2,
        date: '2023-05-31',
        bedTime: '23:45',
        wakeTime: '07:15',
        duration: '7.5小时',
        quality: 3
      },
      {
        id: 3,
        date: '2023-05-30',
        bedTime: '00:15',
        wakeTime: '08:00',
        duration: '7小时45分',
        quality: 5
      }
    ]);
    
    const loading = ref(false);
    const loadingText = ref({
      contentdown: '上拉显示更多',
      contentrefresh: '加载中...',
      contentnomore: '没有更多数据了'
    });
    
    // 当前月份
    const currentMonth = computed(() => {
      const date = new Date();
      return `${date.getFullYear()}年${date.getMonth() + 1}月`;
    });
    
    // 平均睡眠时长
    const averageDuration = computed(() => {
      return sleepRecords.value.length > 0 ? '7.8小时' : '0小时';
    });
    
    // 平均睡眠质量
    const averageQuality = computed(() => {
      if (sleepRecords.value.length === 0) return '0';
      const sum = sleepRecords.value.reduce((acc, record) => acc + record.quality, 0);
      return (sum / sleepRecords.value.length).toFixed(1);
    });
    
    // 记录天数
    const recordCount = computed(() => {
      return sleepRecords.value.length;
    });
    
    // 格式化日期-日
    const formatDay = (dateStr) => {
      const date = new Date(dateStr);
      return date.getDate().toString();
    };
    
    // 格式化日期-月
    const formatMonth = (dateStr) => {
      const date = new Date(dateStr);
      const month = date.getMonth() + 1;
      return `${month}月`;
    };
    
    // 添加睡眠记录
    const addRecord = () => {
      uni.navigateTo({
        url: '/pages/record/add'
      });
    };
    
    // 处理标签点击
    const handleTabClick = (tab) => {
      switch (tab) {
        case 'home':
          wx.navigateTo({ url: '/pages/index/index' });
          break;
        case 'survey':
          wx.navigateTo({ url: '/pages/survey/index' });
          break;
        case 'record':
          // 当前已在记录页，不需要跳转
          break;
        case 'knowledge':
          wx.navigateTo({ url: '/pages/knowledge/index' });
          break;
        case 'my':
          wx.navigateTo({ url: '/pages/my/index' });
          break;
      }
    };
    
    // 获取睡眠记录
    const getSleepRecords = async () => {
      // 实际开发中应该调用接口获取数据
    };
    
    onMounted(() => {
      getSleepRecords();
    });
    
    return {
      sleepRecords,
      loading,
      loadingText,
      currentMonth,
      averageDuration,
      averageQuality,
      recordCount,
      formatDay,
      formatMonth,
      addRecord,
      handleTabClick
    };
  },
  // 添加微信小程序原生方法定义
  methods: {
    handleTabClick(tab) {
      console.log('handleTabClick called with:', tab);
      switch (tab) {
        case 'home':
          console.log('Navigating to home page');
          uni.navigateTo({ url: '/pages/index/index' });
          break;
        case 'survey':
          console.log('Navigating to survey page');
          uni.navigateTo({ url: '/pages/survey/index' });
          break;
        case 'record':
          // 当前已在记录页，不需要跳转
          console.log('Already on record page');
          break;
        case 'knowledge':
          console.log('Navigating to knowledge page');
          uni.navigateTo({ url: '/pages/knowledge/index' });
          break;
        case 'my':
          console.log('Navigating to my page');
          uni.navigateTo({ url: '/pages/my/index' });
          break;
      }
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f5f5;
}

.content {
  flex: 1;
  padding: 20rpx;
  padding-bottom: 120rpx;
}

.stats-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.08);
}

.stats-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.stats-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.stats-date {
  font-size: 26rpx;
  color: #999;
}

.stats-body {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stats-item {
  flex: 1;
  text-align: center;
}

.stats-value {
  font-size: 36rpx;
  font-weight: bold;
  color: #2979FF;
  display: block;
  margin-bottom: 10rpx;
}

.stats-label {
  font-size: 24rpx;
  color: #999;
}

.record-list {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.08);
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.add-record {
  display: flex;
  align-items: center;
  color: #2979FF;
  font-size: 26rpx;
}

.add-icon {
  font-size: 20rpx;
  margin-right: 8rpx;
}

.record-item {
  margin-bottom: 20rpx;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 20rpx;
}

.record-item:last-child {
  margin-bottom: 0;
  border-bottom: none;
  padding-bottom: 0;
}

.record-card {
  display: flex;
  align-items: center;
}

.record-date {
  width: 100rpx;
  text-align: center;
  margin-right: 20rpx;
}

.date-day {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
  display: block;
}

.date-month {
  font-size: 24rpx;
  color: #999;
  display: block;
}

.record-info {
  flex: 1;
}

.sleep-time, .sleep-duration {
  display: flex;
  align-items: center;
  margin-bottom: 10rpx;
}

.sleep-time:last-child, .sleep-duration:last-child {
  margin-bottom: 0;
}

.time-label, .duration-label {
  width: 120rpx;
  font-size: 26rpx;
  color: #999;
}

.time-value, .duration-value {
  font-size: 26rpx;
  color: #333;
}

.record-quality {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 150rpx;
}

.quality-label {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 10rpx;
}

.quality-stars {
  display: flex;
}

.star-icon {
  font-size: 16rpx;
  margin-right: 4rpx;
}

.empty-tip {
  text-align: center;
  padding: 60rpx 0;
}

.empty-tip text {
  color: #999;
  font-size: 28rpx;
  display: block;
  margin-bottom: 30rpx;
}

.btn-primary {
  background-color: #2979ff;
  color: #fff;
  font-size: 28rpx;
  padding: 16rpx 40rpx;
  border-radius: 100rpx;
  border: none;
  margin-top: 20rpx;
}

.loading-more {
  padding: 20rpx 0;
  text-align: center;
}

.loading-content {
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-text {
  color: #999;
  font-size: 26rpx;
}

.tab-bar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100rpx;
  background-color: #fff;
  border-top: 1px solid #eee;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10rpx 0;
}

.tab-icon {
  font-size: 32rpx;
  color: #666;
  margin-bottom: 8rpx;
  line-height: 1;
}

.tab-text {
  font-size: 20rpx;
  color: #666;
  line-height: 1;
}

.tab-item.active .tab-text {
  color: #2979ff;
}
</style> 